package cn.jetpiece.cloud.system.dp.app.modular.biz.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.jetpiece.cloud.system.dp.api.biz.model.params.CreateDpTScopeDataParam;
import cn.jetpiece.cloud.system.dp.app.modular.biz.entity.DpTScopeData;
import cn.jetpiece.cloud.system.dp.app.modular.biz.mapper.DpTScopeDataMapper;
import cn.jetpiece.cloud.system.dp.app.modular.biz.service.IDpTScopeDataService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * dp_t_scope_data
 *
 * @author jetpiece
 * @since 2023-05-18
 */
@Service
public class DpTScopeDataServiceImpl extends ServiceImpl<DpTScopeDataMapper, DpTScopeData> implements IDpTScopeDataService {

    @Override
    public DpTScopeData getByAppKey(String appKey) {
        if(StringUtils.isBlank(appKey)){
            return null;
        }
        LambdaQueryWrapper<DpTScopeData> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(DpTScopeData::getAppKey,appKey);
        List<DpTScopeData> scopeDataList = this.list(queryWrapper);
        if(CollUtil.isNotEmpty(scopeDataList)){
            return scopeDataList.get(0);
        }
        return null;
    }

    @Override
    public boolean add(CreateDpTScopeDataParam param) {
        DpTScopeData scopeData = this.getByAppKey(param.getAppKey());
        String projectId = param.getProjectValue()+",";
        if(ObjectUtil.isNull(scopeData)){
            scopeData = new DpTScopeData();
            scopeData.setAppKey(param.getAppKey());
            scopeData.setProjectValue(projectId);
            this.save(scopeData);
            return true;
        }
        List<String> scopeProList = new ArrayList<>();
        if(StringUtils.isNotBlank(scopeData.getProjectValue())){
            scopeProList.addAll(Arrays.asList(scopeData.getProjectValue().split(",")));
            if(!scopeProList.contains(projectId)){
                scopeProList.add(projectId);
            }
        }else{
            scopeProList.add(projectId);
        }
        scopeData.setProjectValue(scopeProList.stream().collect(Collectors.joining(",")));
        this.updateById(scopeData);
        return true;
    }

    @Override
    public boolean addApi(CreateDpTScopeDataParam param) {
        DpTScopeData scopeData = this.getByAppKey(param.getAppKey());
        String apiId = param.getApiValue()+",";
        if(ObjectUtil.isNull(scopeData)){
            scopeData = new DpTScopeData();
            scopeData.setAppKey(param.getAppKey());
            scopeData.setApiValue(apiId);
            this.save(scopeData);
            return true;
        }
        List<String> scopeApiList = new ArrayList<>();
        if(StringUtils.isNotBlank(scopeData.getApiValue())){
            scopeApiList.addAll(Arrays.asList(scopeData.getApiValue().split(",")));
            if(!scopeApiList.contains(apiId)){
                scopeApiList.add(apiId);
            }
        }else{
            scopeApiList.add(apiId);
        }
        scopeData.setApiValue(scopeApiList.stream().collect(Collectors.joining(",")));
        this.updateById(scopeData);
        return true;
    }
}
